;THIS IS A DEMO OF HOW TO USE
;---------------------------------------
#NoEnv
SendMode, Input
SetBatchLines, -1
SetWorkingDir, %a_scriptdir%
;---------------------------------------
#Include, %a_scriptdir%\BLOCKWORLD.ahk
#Include, %a_scriptdir%\BLOCKWORLD_XML.ahk
OnExit, Exit

;===========================
; OTHER SETTINGS
;---------------------------
 scriptname     := "Blockworld Editor"
 ver            := 0.1
 GL_XPOS        := 500
 GL_YPOS        := 300
 GL_Size_X      := 640/4
 GL_Size_Y      := 480/4
;===========================


SUPPORTEDFORMATS    := "bmp,gif,png,jpg,jpeg"


;############# WE BUILD OUR GUI #########################
Gui, font, bold s14
Gui, add, Text, x20 y30, Textures
Gui, font,
Gui, add, Button, xp yp+30 w100 h20 gCREATE_TEXTURE_FROM_FOLDER_XML,[.create xml]
Gui, add, Button, xp yp+30 w100 h20 gCREATE_ANIMATION_FROM_FOLDER_XML,[.create animation]
Gui, add, Button, xp yp+30 w100 h20 gCREATE_CHARCTER_ANIMATION_FROM_FOLDER_XML,[.create character]

Gui, add, Text, x%GL_XPOS% y%GL_YPOS% w%GL_Size_X% h%GL_Size_Y% vGLControl hwndGLControl

Gui, add, Statusbar
Gui, show, w800 h500, % Windowname " ver " ver


BLOCKWORLD_INIT(GLControl,GL_Size_X,GL_Size_Y)
BLOCKWORLD_LOAD_MAP()

SB_SetText("Editor ready")
return




CREATE_TEXTURE_FROM_FOLDER_XML:
  FileSelectFolder,texture_folder,%a_scriptdir%,,Select Folder with textures
  If(errorlevel){
   return
  }

  BW_XML_TEXTURE_CREATE(NEW_TEXTURES)

  Loop, %texture_folder%\*.*,0,1
  {
   If A_LoopFileExt not in %SUPPORTEDFORMATS%
    Continue
    
   StringReplace,real_texturepath,A_LoopFileFullPath,%texture_folder%  ;get relative path
   SplitPath, A_LoopFilename, , , , OutNameNoExt
   BW_XML_TEXTURE_ADD(NEW_TEXTURES,OutNameNoExt,real_texturepath)
  }
  ;we are done
  Msgbox,64,Erfolgreich Texturen erstellt, % "Es wurden "  BW_XML_TEXTURE_COUNT(NEW_TEXTURES) " Texturen gefunden."
  
  BW_XML_SAVE(NEW_TEXTURES,texture_folder "\textures.xml")
return

;*************************************************************************************
CREATE_ANIMATION_FROM_FOLDER_XML:
 FileSelectFolder,texture_folder,%a_scriptdir%\data\textures,,Select Folder with animation textures
 If(ErrorLevel){
  return
 }
  
 InputBox,ANIMATIONNAME,Create Animation, Enter name of this animation:
  
 CreateAnimation(ANIMATIONNAME,texture_folder)

 Msgbox,0,Note, Animation sucessfull created!
return

;########################################################################


CreateAnimation(ANIMATIONNAME,animation_folder){
global 
 xmlanimpath := a_scriptdir . "\data\animation.xml"
 If(fileexist(xmlanimpath) = ""){
  BW_XML_ANIMATION_CREATE(NEW_ANIMATION)
 }else{
  BW_XML_ANIMATION_OPEN(NEW_ANIMATION,xmlanimpath)
 }
 liste := ""
   Loop, %animation_folder%\*.*,0
   {
      If A_LoopFileExt not in %SUPPORTEDFORMATS%
       Continue
      SplitPath, A_LoopFilename, , , , OutNameNoExt
     liste .= OutNameNoExt "|"
  }
 
 Sort, liste, D|
 
 BW_XML_ANIMATION_ADD(NEW_ANIMATION,ANIMATIONNAME,liste)
 
 BW_XML_SAVE(NEW_ANIMATION,xmlanimpath)

return, 1
}




CREATE_CHARCTER_ANIMATION_FROM_FOLDER_XML:
 FileSelectFolder,texture_folder,%a_scriptdir%\data\textures,,Select Folder with character textures
  If(ErrorLevel){
  return
 }
 xmlanimpath := a_scriptdir . "\data\animation.xml"
 InputBox,CHARNAME,Create Animation, Enter name of this Character:
 
 Loop, %texture_folder%\*.*,2
 {  ;for ech folder (sub animation of the char)
   If(!instr(A_LoopFileName, ".")){
    CreateAnimation(CHARNAME A_LoopFileName,A_LoopFileFullPath)
  }
 }
  
  
  
return



BLOCKWORLD_EVENT(obj,event){
 return, 1
 }

leading_zeros_str(str,n){
	pos 	:= 1
	START	:= 1
	new_str	:= ""
	Loop
	{
		pos := RegExMatch(str,"([0-9]+)",out,pos)
		If(!pos){
			break
		}else{
			pos := pos + StrLen(out1)
			new_value := leading_zeros(out1,n)
			new_str .= substr(str,START,(pos - START) -1) . new_value
			START := pos + strlen(out1)
		}
	}
	
	;RESTEN SUCHEN
	new_str .= SUBSTR(STR,START)

	return, new_str
}



leading_zeros(number,n){
	SetFormat,float,03.0
	
	If(!instr(number,".")){
		number .= ".0"
	}
	return, (number + 0)
}



#Include, %a_scriptdir%\BLOCKWORLD_FOOTER.ahk




